package com.lianyu507.api;


import com.lianyu507.annotation.UserWordsCount;
import com.lianyu507.enums.ErrorTypeEnum;
import com.lianyu507.pojo.CrWordTranslate;
import com.lianyu507.pojo.ShUserWord;
import com.lianyu507.pojo.ShUserWordDetails;
import com.lianyu507.pojo.ShUserWordExample;
import com.lianyu507.service.ShUserWordService;
import com.lianyu507.service.WordShowDomainService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
import java.util.Map;


//http://localhost:8081/ee-portal/html/v1/get/word?userId=1&wordId=1
//领域驱动模型    自己写本项目的api  commit
@Controller
@RequestMapping("/html/v1")
public class ShUserWordController extends BaseController {



	@Autowired
	private WordShowDomainService wordShowDomainService;
	@Autowired
	private ShUserWordService shUserWordService;

	/**
	 * 马西亚
	 * @param userId
	 * @param status
	 * @return
	 */
    @RequestMapping("/selectWordsIsRemeber")
    @ResponseBody
    public Map<String, Object> findWordsIsRemeber(Integer userId, byte status) {
        try {
            List<ShUserWord> shUserWords = wordShowDomainService.selectWordByIsRemeber(userId, status);
            return successResult(shUserWords);
        } catch (Exception e) {
            return failedResult(ErrorTypeEnum.NOT_FOUND, e.getMessage());
        }
    }


	/**
	 * 杨亮
	 * @param userId
	 * @return
	 */
	@RequestMapping("/get/all/word")
    @ResponseBody
    public Map<String, Object> findAllWord(Integer userId) {
        
        List<CrWordTranslate> words = wordShowDomainService.getAllWordByUserId(userId);

		return this.successResult(words);
	}


	/**
	 * 单词翻译  陈俊洁
	 * @return
	 */
	@RequestMapping(value = "/chinese",method = RequestMethod.POST)
	@ResponseBody
	public Map<String,Object> findChineseWord(@RequestBody CrWordTranslate crWordTranslate){
		try {
			crWordTranslate=wordShowDomainService.getCrWordTranslate(crWordTranslate);
			return this.successResult(crWordTranslate);
		} catch (Exception e) {
			return this.failedResult(ErrorTypeEnum.SERVER_ERROR, e.getMessage());
		}
	}

	/**
	 * @param shUserWord 曹俊杰
	 * @return 添加/更新单词 通过单词id添加
	 */
	@RequestMapping("/addOrUpdateWord")
	@ResponseBody
	@UserWordsCount
	public Map<String, Object> addOrUpdateWord(@RequestBody ShUserWord shUserWord){
		try {
			wordShowDomainService.addOrUpdateWordByshUserWordByWordId(shUserWord);
		}catch (Exception e){
			return failedResult(ErrorTypeEnum.SERVER_ERROR,e.getMessage());
		}
		return successResult(true);
	}

	/**
	 * @param shUserWord 曹俊杰
	 * @return 添加/更新单词 通过单词word添加单词
	 */
	@RequestMapping("/addOrUpdateWord2")
	@ResponseBody
	@UserWordsCount
	public Map<String, Object> addOrUpdateWord2(@RequestBody ShUserWord shUserWord, String english){
		try {
			wordShowDomainService.addOrUpdateWordByshUserWordByEnglish(shUserWord,english);
		}catch (Exception e){
			return failedResult(ErrorTypeEnum.SERVER_ERROR,e.getMessage());
		}
		return successResult(true);
	}
	
	/**
	 * @param : userId
	 * @return 生词列表
	 * @Date: 14:35 2017/11/23
	 */
	@RequestMapping("/getlist")
	@ResponseBody
	public Map<String, Object> getWordList(Integer userId){
		ShUserWordExample example = new ShUserWordExample();
		example.or().andUserIdEqualTo(userId);
		List<ShUserWord> shUserWords = shUserWordService.selectByExample(example);
		return successResult(shUserWords);
	}

	/**
	 * 查询登录用户的生词本各生词的点击量
	 * (包括生词的中英文名, 及点击量, 按点击量降序排列)
	 * @param userId
	 * @return
	 * @auth: hailunZhao
	 */
	@RequestMapping("/getWordsCount")
	@ResponseBody
	public Map<String, Object> getWordsCountListByUserId(Integer userId) {
		List<ShUserWordDetails> words = wordShowDomainService.queryWordsAndCountByUserId(userId);
		return successResult(words);
	}


	
}
